package me.sluijsens.StartersQuiz;

import net.milkbowl.vault.permission.Permission;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;

/**
 * Commands are managed and executed here
 * @author Bryan Slop (Sluijsens)
 *
 */
public class CmdExecutor implements CommandExecutor {
	private Permission permission;
	private StartersQuiz plugin;
	private String tagChat;
	private LangHandler lang;
	private QuizHandler quizHandler;
	private ConfigHandler conf;
	
	/**
	 * @param instance Instance of the plugin
	 */
	public CmdExecutor(StartersQuiz instance) {
		plugin = instance;
	}
	
	/**
	 * Managing and executing commands
	 * @return Returns true on successful command
	 */
	@SuppressWarnings({ "static-access", "unused" })
	@EventHandler(priority = EventPriority.NORMAL)
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		lang = plugin.getLangHandler();
		tagChat = plugin.tagChat;
		quizHandler = plugin.getQuizHandler();
		conf = plugin.getConf();
		permission = plugin.permission;
		
		boolean commandOnChat = conf.getBoolean("CommandOnChat", false);
		if(cmd.getName().equalsIgnoreCase("sqreload")) {
			if(sender instanceof Player && permission.has(((Player) sender).getWorld().getName(), ((Player) sender).getName(), "sq.reload") || sender.getName().equalsIgnoreCase("console")) {
				String msg;
				
				if(plugin.load()) {
					msg = "Reloaded config, quiz and player files succesfully!";
				} else {
					msg = "Something went wrong when reloading. Use /sqreload (again). Else, take a look at the server.log file.";
				}
				
				sender.sendMessage(tagChat + msg);
			} else {
				sender.sendMessage(tagChat + "You do not have the permission to do that.");
			}
			return true;
		} else if(sender instanceof Player) {
			//Define player, location and world
			Player player = (Player) sender;
			Location loc = player.getLocation();
			World w = loc.getWorld();
			
			if(cmd.getName().equalsIgnoreCase("sq") || cmd.getName().equalsIgnoreCase("sqhelp")) {//This command is used to show the user the help page.
				player.sendMessage(ChatColor.GOLD + "--------------------" + ChatColor.AQUA + "*StartersQuiz*" + ChatColor.GOLD + "--------------------");
				player.sendMessage(ChatColor.GOLD + lang.getMessage("helpArguments"));
				player.sendMessage(ChatColor.AQUA + "/sq(help) " + ChatColor.GOLD + lang.getMessage("sqhelp"));
				player.sendMessage(ChatColor.AQUA + "/sqstart " + ChatColor.GOLD + lang.getMessage("sqstart"));
				player.sendMessage(ChatColor.AQUA + "/sqquestion " + ChatColor.GOLD + lang.getMessage("sqquestion"));
				player.sendMessage(ChatColor.AQUA + "/sqanswer <A|B|C|D|etc.> " + ChatColor.GOLD + lang.getMessage("sqanswer"));
				if(commandOnChat) {
					player.sendMessage(ChatColor.GOLD + lang.getMessage("AnswerOnChat"));
				}
				player.sendMessage(ChatColor.AQUA + "/sqretry " + ChatColor.GOLD + lang.getMessage("sqretry"));
				player.sendMessage(ChatColor.GOLD + "-----------------------------------------------------");
				return true;
			} else if(cmd.getName().equalsIgnoreCase("sqstart")) {
				quizHandler.start(player);
				return true;
			} else if(cmd.getName().equalsIgnoreCase("sqquestion")) {
				quizHandler.show(player);
				return true;
			} else if(cmd.getName().equalsIgnoreCase("sqanswer")) {
				if(args.length > 0) {
					String givenAnswer = args[0];
				
					quizHandler.answer(givenAnswer, player);
				} else {
					player.sendMessage(tagChat + lang.getMessage("NoAnswerGiven"));
				}
				return true;
			} else if(cmd.getName().equalsIgnoreCase("sqretry")) {
				quizHandler.retry(player, false);
				return true;
			}
		} else {
			sender.sendMessage(tagChat + "Only players are allowed to use that command.");
			return true;
		}
		return false;
	}
}
